//
//  ReleaseCell.m
//  BestPurchase
//
//  Created by ZSMAC on 2018/10/10.
//  Copyright © 2018年 张帅. All rights reserved.
//

#import "ReleaseCell.h"
@interface ReleaseCell()<UITextViewDelegate>
@property(nonatomic,retain)UITextField * priceField;
@property(nonatomic,retain)UITextView *titleTextView;
@property(nonatomic,retain)UILabel * placeholderLabel;
@property(nonatomic,retain)UIButton * addBtn;
@property(nonatomic,retain)UIImageView * showImageView;
@property(nonatomic,retain)UIImage * showCurrentImage;
@property(nonatomic,retain)UIButton * deleteBtn;
@end
@implementation ReleaseCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self=[super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self uiConfigure];
    }
    return self;
}
- (void)uiConfigure {
    self.priceField = [[UITextField alloc] initWithFrame:CGRectMake(15, 15, SCREEN_WIDTH-30, 50)];
    self.priceField.placeholder=@"请输入价格￥";
    self.priceField.borderStyle=UITextBorderStyleNone;
    self.priceField.textColor=[UIColor redColor];
    self.priceField.font=[UIFont boldSystemFontOfSize:16];
    self.priceField.keyboardType=UIKeyboardTypeDecimalPad;
    [self.priceField addTarget:self action:@selector(textChengd) forControlEvents:UIControlEventEditingChanged];
    [self.contentView addSubview:self.priceField];
    
    self.titleTextView = [[UITextView alloc] initWithFrame:CGRectMake(15, 15, SCREEN_WIDTH-30, 50)];
    self.titleTextView.font=[UIFont systemFontOfSize:15];
    self.titleTextView.delegate=self;
    [self.contentView addSubview:self.titleTextView];
    
    _placeholderLabel = [[UILabel alloc] initWithFrame:CGRectMake(3, 8, self.titleTextView.width-5, self.titleTextView.height-10)];
    _placeholderLabel.font=Font(16);
    _placeholderLabel.numberOfLines=0;
    _placeholderLabel.textColor=MyRGB(153, 153, 153);
    [self.titleTextView addSubview:_placeholderLabel];
    
    _addBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _addBtn.frame=CGRectMake(0, 0, SCREEN_WIDTH, 80);
    [_addBtn setTitle:@"点击添加文字描述或内容" forState:UIControlStateNormal];
    [_addBtn setTitleColor:[UIColor colorFromHex:@"#0074FF" alpha:1] forState:UIControlStateNormal];
    [_addBtn addTarget:self action:@selector(addBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:_addBtn];
    
    _showImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 5, SCREEN_WIDTH, SCREEN_WIDTH-30)];
    _showImageView.backgroundColor=[UIColor colorWithRed: arc4random_uniform(256)/255.0f green: arc4random_uniform(256)/255.0f blue: arc4random_uniform(256)/255.0f alpha:1];
    _showImageView.userInteractionEnabled=YES;
    [self.contentView addSubview:_showImageView];
    
    _deleteBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _deleteBtn.frame=CGRectMake(SCREEN_WIDTH-30, 0, 30, 30);
    [_deleteBtn setImage:[UIImage imageNamed:@"btn_del_nor"] forState:UIControlStateNormal];
    [_deleteBtn addTarget:self action:@selector(deleteBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
    
   
    
}
#pragma mark 输入框
- (void)textChengd {
    _cellModel.content=self.priceField.text;
}
#pragma mark UITextViewDelegate
- (void)textViewDidChange:(UITextView *)textView {
    if (textView.text.length==0) {
        _placeholderLabel.hidden=NO;
    } else {
        _placeholderLabel.hidden=YES;
    }
    _cellModel.content=textView.text;
    CGSize size= [textView sizeThatFits:CGSizeMake(textView.width, CGFLOAT_MAX)];
    textView.height=size.height;
    if (self.editorRefreshCellHeightBolck) {
        self.editorRefreshCellHeightBolck(textView.bottom);
    }

}
- (void)showObj:(UIView *)showObj {
    for (UIView *subView in self.contentView.subviews) {
        if (showObj == subView) {
            subView.hidden=NO;
        } else {
             subView.hidden=YES;
        }
    }
}
- (void)setCellModel:(ReleaseCellModel *)cellModel {
    _cellModel=cellModel;
    
    self.titleTextView.text=_cellModel.content;
    if (_cellModel.placeholderStr.length!=0) {
        self.placeholderLabel.hidden=NO;
        if (_cellModel.content.length!=0) {
             self.placeholderLabel.hidden=YES;
        }
        self.placeholderLabel.text=_cellModel.placeholderStr;
        [self.placeholderLabel sizeToFit];
    }
    
    if (_cellModel.showType==Show_Price_Type) {
        [self showObj:self.priceField];
    }
    if (_cellModel.showType==Show_Title_Type) {
         [self showObj:self.titleTextView];
         self.titleTextView.font=[UIFont boldSystemFontOfSize:16];
    }
    if (_cellModel.showType==Show_textContent_Type) {
        [self showObj:self.titleTextView];
        [self.contentView addSubview:_deleteBtn];
    }
    if (_cellModel.showType==Show_Image_Type) {
        [_showImageView addSubview:_deleteBtn];
        [self showObj:self.showImageView];
        self.showCurrentImage=_cellModel.image;
    }
    if (_cellModel.showType==Show_Add_Type) {
        [self showObj:self.addBtn];
    }
    
    
    if (self.getCellHeight) {
         CGFloat height = 50;
        for (UIView *subView in self.contentView.subviews) {
            if (!subView.hidden) {
                height=subView.bottom;
            }
        }
        self.getCellHeight(height);
    }

}
-(void)setShowCurrentImage:(UIImage *)showCurrentImage {
    _showCurrentImage=showCurrentImage;
   self.showImageView.image=_showCurrentImage;
   CGFloat proportion = _showCurrentImage.size.height/ _showCurrentImage.size.width;//高除以宽 得出比例
   self.showImageView.height= _showImageView.width *proportion;
}
- (void)addBtnClicked:(UIButton *)btn {
    if (self.addContentBlock) {
        self.addContentBlock();
    }
}

- (void)deleteBtnClicked:(UIButton *)btn {
    if (self.deleteBlock) {
        self.deleteBlock(_indexPath);
    }
}
- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
